<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class City_m extends CI_Model {

	const TBL = 'city_info';

	/**
     * 构造函数
     *
     * @access public
     * @return void
     */
    public function __construct()
    {
       parent::__construct();
	   $this->load->model('Goods_m','good');
    }

	function get_city_by_id($id)
	{
		$this->db->where('id', $id);
		$query = $this->db->get(self::TBL);
		$row = $query->row_array();
		return $query->num_rows() ? $row['city'] : false;
	}

	function get_id_by_city($city)
	{
		$this->db->where('city', $city);
		$query = $this->db->get(self::TBL);
		$row = $query->row_array();
		return $query->num_rows() ? $row['id'] : false;
	}

	function update_city($id, $name){
		$this->db->where('id', $id);
		$this->db->update(self::TBL, array('city' => $name));
	}

	function get_city_by_ids($ids){
		$this->db->select('id, city');
		$this->db->where_in('id', $ids);
		$query = $this->db->get(self::TBL);
		if ($query){
			$res = array();
			foreach ($query->result() as $ires){
				$res[] = array('id' => $ires->id, 'city' => $ires->city); 
			}
			return $res;
		} else {
			return FALSE;
		}
	}

	function get_paged_city($offset = 0, $row_count = 0, $name = ''){
		$this->db->select('id, city');
		if (strlen($name) > 0)
		{
			$this->db->like('city', $name, 'both');
		}
		$this->db->order_by('id desc');
		return $this->db->get(self::TBL, $row_count, $offset);
	}

	function get_total_city($name){
		$this->db->select("COUNT(*) AS total", FALSE);
		if (strlen($name) > 0)
		{
			$this->db->like('city', $name, 'both');
		}
		$query = $this->db->get(self::TBL);
		$res = $query->result_array();
		return intval($res[0]['total']);
	}

	function get_all(){
		$ret = array();
		$this->db->where('city <>', '全国');
		$this->db->order_by("id", "asc");
		$query = $this->db->get(self::TBL);
		foreach ($query->result_array() as $row){
			$item = array();
			$item['city_id'] = $row['id'];
			$item['city'] = $row['city'];
			$ret[] = $item;
		}
		return $ret;
	}
	
	function get_all_from_config(){
		$ret = array();
		$all_city = $this->get_all();
		$pre_city = unserialize (PRE_CITY);
		foreach($all_city as $city_item){
			if (in_array($city_item['city'], $pre_city) && $city_item['city'] <> '全国') {
				$ret[] = $city_item;
			}
		}
		return $ret;
	}
	
	function get_all_map(){
		$ret = array();
		$query = $this->db->get(self::TBL);
		foreach ($query->result_array() as $row){
			$ret[$row['id']] = $row['city'];
		}
		return $ret;
	}

	function get_city_info($cities = array()){
		$ret = array();

		$all_info = array();
		$this->db->where_in('city', $cities);
		$query = $this->db->get(self::TBL);
		foreach ($query->result_array() as $row){
			$item = array();
			$item['city_id'] = $row['id'];
			$item['city'] = $row['city'];
			$all_info[] = $item;
		}

		foreach ($cities as $city){
			foreach ($all_info as $city_info){
				if($city == $city_info['city']){
					$ret[] = $city_info;
				}
			}
		}

		return $ret;
	}

	function add_city($city){

		if(is_string($city) && $city){
			$this->db->set('city', $city);
			if($this->db->insert(self::TBL)){
				return $this->db->insert_id();
			}
		}

		return false;
	}

	function get_city_id_whith_add($city){
		if(!is_string($city) || !$city){
			return false;
		}

		$city_id = $this->get_id_by_city($city);
		if($city_id){
			return $city_id;
		}

		return $this->add_city($city);
	}

	function delete_city($id){
		$good_count = $this->good->is_city_with_goods($id);
		if ($good_count > 0){
			return FALSE;
		} else {
			$this->db->delete(self::TBL, array('id' => $id));	
			return TRUE;
		}
	}

	function set_city_info_in_session($city_id, $city){
		$this->session->set_userdata('city_id',$city_id);
		$this->session->set_userdata('city',$city);
	}
}

/* End of file users_mdl.php */
/* Location: ./application/models/users_mdl.php */
